package com.cjl.netty.webcocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 初始化ChannelChildHandler连接
 * </p>
 *
 * @author cjl
 * @version 1.0
 * @since 2020-07-30 15:30
 */
@Component
public class NettyWebsocketChildHandlerInitializer extends ChannelInitializer<SocketChannel> {


    public NettyWebsocketChildHandlerInitializer() {
        super();
    }
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // HTTP编解码器
        socketChannel.pipeline().addLast("http-codec", new HttpServerCodec());
        // HTTP头和body拼接成完整请求体
        socketChannel.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        // 大文件传输策略
        socketChannel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        socketChannel.pipeline().addLast("websocket-handler",new NettyWebSocketServerHandler()) ;

    }
}